package com.yy.wmmaccmsapp.main

import android.app.DownloadManager
import android.util.Log
import androidx.lifecycle.MutableLiveData
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import com.yy.wmmaccmsapp.net.apiService
import kotlinx.coroutines.*
import me.hgj.jetpackmvvm.ext.download.DownLoadManager
import me.hgj.jetpackmvvm.ext.download.OnDownLoadListener
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState
import java.io.BufferedInputStream
import java.io.BufferedReader
import java.io.File
import java.io.FileReader

class MainViewModel : BaseViewModel() {
    var title = ""

    var kamiInfo = MutableLiveData<ResultState<Any>>()

    var updateInfoUrl = MutableLiveData<String>()

    var updateDownloadProgress = MutableLiveData<Int>()


    var appStartInstall = MutableLiveData<String>()

    fun getKamiInfo(cardPasswd: String) {
        request({ apiService.getKamiInfo(cardPasswd) }, kamiInfo, true)
    }


    /**
     * 检查app更新
     */
    fun checkAppUpdate(savePath: String, code: Int) {
        GlobalScope.launch {
            DownLoadManager.downLoad(
                "version.txt",
                "http://64.32.22.196:82/download/1.txt",
                savePath,
                "version.txt",
                true,
                object : OnDownLoadListener {
                    override fun onDownLoadPrepare(key: String) {
                        Log.e("zyj...", "...onDownLoadPrepare .... ")
                    }

                    override fun onDownLoadError(key: String, throwable: Throwable) {
                        Log.e("zyj...", "....onDownLoadError.... ")
                    }

                    override fun onDownLoadSuccess(key: String, path: String, size: Long) {
                        Log.e("zyj...", "....key....$key...path .... $path .... ")

                        var file = File(path)
                        var br = BufferedReader(FileReader(file))
                        var newCode = br.readLine().toInt()
                        var appUrl = br.readLine()
                        br.close()
                        Log.e("zyj...", "....newCode....$newCode...appUrl .... $appUrl .... ")
                        if (code < newCode) {
                            //app有更新
                            updateInfoUrl.value = appUrl
                        }else{
                            updateInfoUrl.value = "no update"
                        }

                    }

                    override fun onDownLoadPause(key: String) {
                        Log.e("zyj...", "....onDownLoadPause .... ")
                    }

                    override fun onUpdate(
                        key: String,
                        progress: Int,
                        read: Long,
                        count: Long,
                        done: Boolean
                    ) {
                        Log.e("zyj...", "...onUpdate. ")
                    }

                })
        }
    }


    fun doAppUpdate(savePath: String, url: String) {
        GlobalScope.launch {
            DownLoadManager.downLoad(
                "version.apk",
                url,
                savePath,
                "version.apk",
                true,
                object : OnDownLoadListener {
                    override fun onDownLoadPrepare(key: String) {
                        Log.e("zyj...", "...onDownLoadPrepare .... ")
                    }

                    override fun onDownLoadError(key: String, throwable: Throwable) {
                        Log.e("zyj...", "....onDownLoadError.... ")
                    }

                    override fun onDownLoadSuccess(key: String, path: String, size: Long) {
                        Log.e("zyj...", "....key....$key...path .... $path .... ")

//                    var file = File(path)
//                    var br = BufferedReader(FileReader(file))
//                    var newCode = br.readLine().toInt()
//                    var appUrl = br.readLine()
//                    br.close()
//                    if(code < newCode){
//                        //app有更新
//                        updateInfoUrl.value = appUrl
//                    }

                        appStartInstall.value = path

                    }

                    override fun onDownLoadPause(key: String) {
                        Log.e("zyj...", "....onDownLoadPause .... ")
                    }

                    override fun onUpdate(
                        key: String,
                        progress: Int,
                        read: Long,
                        count: Long,
                        done: Boolean
                    ) {
                        Log.e("zyj...", "...progress....$progress ..... count.......$count..... ")
                        updateDownloadProgress.value = progress
                    }

                })
        }
    }


}